NumPy - Array

NUMPY

 
In my previous article, I introduced you to NumPy. We learned  what NumPy is, how it can be installed, and we worked with some NumPy operations.
 
To learn NumPy, we need to understand what array is.
 

Arrays

 
NumPy array is a powerful N-dimensional array object which is in the form of rows and columns.
 
Like other programming language, Array is not so popular in Python. In Python, Lists are more popular which can replace the working of an Array or even multiple Arrays, as Python does not have built-in support for Arrays.
 
For LISTS, you can refer to my article and this.
 
But the question is, when to use arrays?
 
Lists are much more flexible than arrays. They can store elements of different data types including string. Also, lists are faster than arrays. But, if you need to do mathematical computation on arrays and matrices, you are much better off using something like the NumPy library.
 
Also, Python NumPy array consumes less memory and is more convenient.
 
Now let us learn, how to work with Array. We can use Lists to create an array.
 

Single Dimension Array

  1. import array as arr    
  2. a = arr.array('d', [1.13.54.5])    
  3. print(a)    
OR
  1. import numpy as np    
  2. a=np.array([1,2,3])    
  3. print(a)    
Note
 
But all the elements in the array must be of the same type (same Data Type).
  1. import array as arr    
  2. a = arr.array('d', [13.5"Hello"])      // Error     
OUTPUT
 
 

Multi-Dimension Array

  1. import numpy as np    
  2. a=np.array([(1,2,3),(4,5,6)])    
  3. print(a)    
Now to iterate and print the same array:
  1. import array as arr    
  2. a = arr.array('d', [1.13.54.5])    
  3. print ("The new created array is : ", end =" ")    
  4. for i in range (03):    
  5. print (a[i], end =" ")    
  6. print()    
OUTPUT
 
 
 
 
Accessing an element in the above array,
  1. import array as arr     
  2. a = arr.array('d', [1.13.54.5])    
  3.       
  4. print ("The new created array is : ", end =" ")     
  5. for i in range (03):     
  6.     print (a[i], end =" ")     
  7. print()     
  8.     
  9. print("Access element is: ", a[0])                  // prints the element at index-0  
  10. print("Access element is: ", a[2])                  // prints the element at index-2  
OUTPUT
 
 
If you are using NumPy to create an array, we can use some shortcuts as well, as NumPy provides you with some in-built functions for the creation of basic arrays:
  1. import numpy as np    
  2. a = np.zeros((2,2))                    //prints 2x2 array of zeros  
  3. print(a)    
  4.     
  5. b = np.ones((1,2))                     //prints 1x2 array of ones  
  6. print(b)    
  7.     
  8. c = np.full((2,2), 7)                  //prints 2x2 array of sevens  
  9. print(c)    
  10.     
  11. d = np.eye(2)                          //prints 2x2 identity array/matrix  
  12. print(d)    
  13.     
  14. e = np.random.random((2,2))            //prints 2x2 array of random numbers  
  15. print(e)    
OUTPUT
 
 
 
 
Adding an element in the above array,
 
SYNTAX
 
a.insert(1, 4) this adds an element-4 at index-1
  1. import array as arr     
  2. a = arr.array('d', [1.13.54.5])    
  3.       
  4. print ("Array is : ", end =" ")     
  5. for i in range (03):     
  6.     print (a[i], end =" ")     
  7. print()     
  8.     
  9. a.insert(12.5)     
  10.       
  11. print ("Array after insertion is: ", end =" ")     
  12. for i in (a):     
  13.     print (i, end =" ")     
  14. print()     
OUTPUT
 
 
 
 
Deleting an element in the above array,
 
SYNTAX
 
a.pop(2); this deletes the element from index-2
a.remove(2); this deletes the element-2
 
Note
 
In case we have a repeated element like, we have two 1’s in an array,  the remove() function will remove the first occurrence if element-1
  1. import array as arr     
  2. a = arr.array('d', [1,2,4,5,1,6,7])    
  3.       
  4. print ("Array is : ", end =" ")     
  5. for i in range (06):     
  6.     print (a[i], end =" ")     
  7. print()     
  8.     
  9. print ("The popped element is : ", end ="")     
  10. print (a.pop(2))     
  11. print ("The array after popping is : ", end ="")     
  12. for i in range (06):     
  13.     print (a[i], end =" ")     
  14.       
  15. print("\r")     
  16.       
  17. a.remove(1)     
  18. print ("The array after removing is : ", end ="")     
  19. for i in range (05):     
  20.     print (a[i], end =" ")     
OUTPUT
 
 
 
 
Updating an element in the above array:
 
SYNTAX
 
arr[2] =6 ;this means that we are assigning element-6 at index-2
  1. import array as arr     
  2. a = arr.array('d', [1.1,2.5,4.5])    
  3.       
  4. print ("Array is : ", end =" ")     
  5. for i in range (03):     
  6.     print (a[i], end =" ")     
  7. print()     
  8.     
  9. a[2] = 3.5    
  10. print("Array after updation : ", end ="")     
  11. for i in range (03):     
  12.     print (a[i], end =" ")     
  13. print()     
OUTPUT
 
 
 
 
Searching for an element in the above array:
 
SYNTAX
 
a.index(2)) ; this gives you the index of the first occurrence of element-2:
  1. import array as arr     
  2. a = arr.array('d', [1.1 ,2.5,3.5,4.5,2.5])    
  3.       
  4. print ("Array is : ", end =" ")     
  5. for i in range (04):     
  6.     print (a[i], end =" ")     
  7. print()     
  8.     
  9. print ("The index of 1st occurrence of 2.5 is : ", end ="")     
  10. print (a.index(2.5))     
OUTPUT
 
 

SUMMARY

 
So we learntedabout NumPy-Array. You got to know how to create, iterate, add, traverse and update elements in array. I hope this article will help you further in Learning Python- NumPy.
 
Feedback or queries related to this article are most welcome.
 
Thanks for reading.


Similar Articles